package at.fhooe.im440.pandora.entities;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import at.fhooe.im440.pandora.collision.CircleCollider;
import at.fhooe.im440.pandora.core.Core;

public class Bullet extends Entity {

	private static final int RANGE = 3000;
	
	private float startX;
	private float startY;
	private float direction;
	private float speed;
	private float positionX;
	private float positionY;
	
	private boolean m_alive = true;
	
	public Bullet(String name, Core core, float startX, float startY, float direction, float speed) {
		super(name, core, startX+2.5f, startY+2.5f, 5.0f);
		this.startX = startX;
		this.startY = startY;
		this.direction = direction;
		this.speed = speed;
		this.positionX = this.startX;
		this.positionY = this.startY;
		core.collisionTester.addBulletObject(getCollisionObject());
	}

	@Override
	public void update() {
		positionX += speed * Math.cos(direction);
		positionY += speed * Math.sin(direction);
		super.updateCollider(positionX+2.5f, positionY+2.5f);
	}

	@Override
	public Point2D getPosition() {
		return new Point2D.Float(this.positionX, this.positionY);
	}

	@Override
	public void setPosition(float x, float y) {
		// nothing to do here, because the position should not be set for bullets		
	}

	@Override
	public void render(Graphics2D g) {
		if(isAlive() && m_alive){
			g.setColor(Color.black);
			g.fillOval((int) positionX, (int) positionY, 10, 10);	
			if(core.isDebug)
			{
				super.drawCollider(g);
			}
		} else {
			core.collisionTester.removeBulletObject(getCollisionObject());
			core.entityManager.removeEntity(this);
		}
	}

	private boolean isAlive() {
		
		return Math.sqrt(Math.pow(startX-positionX, 2) + Math.pow(startY-positionY, 2)) <= RANGE;
	}

	
	//CircleCollider
	@Override
	public void collided(CircleCollider col) {
		m_alive = false;
	}

}
